import React, { Component } from 'react'
import {ListBox} from '../../../components'
import UpdateCustomerPage from './Update'
import Actions from '../ActionStore/Actions';
import { Modal } from 'antd';

export default class ListCustomer extends Component {
    constructor(props) {
        super(props);
        //行操作列表
        this.rowOperation = [
        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'add',
                showName: '新增',
                iconClass: 'plus'
            },{
                handleName: 'edit',
                showName: '编辑',
                iconClass: 'edit'
            },{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            }
        ];
        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'ListCustomerColumnConfig',
            data:[
                {
                    showName: 'id',
                    columnHidden: true,
                    columnName: 'customerId',
                    foreverHidden: true
                },{
                    showName: '客户编码',
                    columnName: 'custCode',
                    columnWidth: 130
                },{
                    showName: '中文描述',
                    columnName: 'zhDesc'
                },{
                    showName: '英文描述',
                    columnName: 'enDesc'
                },{
                    showName: '客户类型',
                    columnName: 'custTypes'
                },{
                    showName: '详细地址',
                    columnName: 'allAddress'
                },{
                    showName: '联系人',
                    columnName: 'contact'
                },{
                    showName: '联系人电话',
                    columnName: 'contactPhone'
                },{
                    showName: '联系人职务',
                    columnName: 'contactPosition'
                },{
                    showName: '联系人邮箱',
                    columnName: 'contactEmail'
                },{
                    showName: '邮编',
                    columnName: 'postcode'
                },{
                    showName: '创建人',
                    columnName: 'createBy'
                },{
                    showName: '创建时间',
                    columnName: 'gmtCreate',
                    columnWidth: 130
                },{
                    showName: '更新人',
                    columnName: 'lastModifiedBy'
                },{
                    showName: '更新时间',
                    columnName: 'gmtModified',
                    columnWidth: 130
                },{
                    showName: '备注',
                    columnName: 'remark',
                    columnWidth: 130
                }
            ]
        };
        //搜索属性配置
        this.searchComponentData = [
            {
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax',
                minName:'gmtCreateMin'
            },{
                name:'gmtModified',
                type: 'date',
                maxName:'gmtModifiedMax',
                minName:'gmtModifiedMin'
            },{
                name : 'custTypes',
                type : 'select',
                url : '/base/dic/get/cust_type'
            }
        ];

    }

    componentWillReceiveProps(nextProps) {
        console.log("==========nextProps.refresh============", nextProps.refresh);
        if(nextProps.refresh != undefined && nextProps.refresh){
            this.refreshHandle();
        }
    }

    rowHandles = (strHandles, rowObj) => {
        console.log("strHandles===ListResource=======", strHandles);
        if(strHandles === 'edit'){
            this.infoHandle(rowObj);
        }else if(strHandles === 'delete'){
            this.deleteHandle(rowObj);
        }else if(strHandles === 'add'){
            this.addHandle();
        }else if(strHandles == 'doubleClick'){
            this.doubleClickHandle(rowObj);
        }
    }

    addHandle = () => {
        console.log("====执行了新增用户操作====");
        let rowObj = [{data:[]}];
        Actions.rowDoubleClick1('1', rowObj);
    }
    editHandle = (rowObj) => {
        console.log("====执行了修改操作====");
        // 只能选择一条限制
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            this.refs.updateCustomer.editHandle(rowObj[0].data)
        }

    }

    deleteHandle = (rowObj) => {
        console.log("=======执行了删除操作====");
        // 只能选择一条限制
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else {
            this.refs.updateCustomer.deleteHandle(rowObj[0].data)
        }

    }

    infoHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            Actions.rowDoubleClick1('0', rowObj);
        }
    }

    doubleClickHandle = (rowObj) => {
        //双击行显示明细，跳转到明细tab，‘2’是定死的key值
        Actions.rowDoubleClick1('0', rowObj);
    }

    //刷新列表
    refreshHandle = () => {
        this.refs.listCustomer.refreshHandle();
    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，test commit
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;
        console.log('================matchedRows================', this.props.matchedRows);
        return(
            <div>
                <ListBox
                    ref = 'listCustomer'
                    isHideLeft={isHideLeft}
                    tableColumnData = {this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url = '/base/customer/list'
                    rowHandles = {this.rowHandles}
                    rowOperation = {this.rowOperation}
                    headerOperation = {this.headerOperation}
                    isRowSingle = "true"
                    subtractHeight = '37'
                    subtractWidth ='0'
                    matchedRows ={this.props.matchedRows}
                />

                <UpdateCustomerPage
                        ref="updateCustomer"
                        refreshHandle = {this.refreshHandle}
                />
            </div>
        )
    }
}
